package mapper

import (
	"context"
	"database/sql"
	"fmt"
	_ "fmt"
	"go-zero-project/services/order/rpc/internal/mapper/entity"
	"time"
)

var sqlAllNotId = "id,uid,pid,amount,status,create_time,update_time"

type OrderMapper struct {
}

func NewOrderMapper() *OrderMapper {
	return &OrderMapper{}
}

func (m *OrderMapper) TxInsert(ctx context.Context, tx *sql.Tx, entity *entity.OrderEntity) (sql.Result, error) {
	now := time.Now()
	query := fmt.Sprintf("insert into %s (%s) values (?,?,?,?,?,?,?)", entity.TableName(), sqlAllNotId)
	ret, err := tx.ExecContext(ctx, query, entity.Id, entity.Uid, entity.Pid, entity.Amount, entity.Status, now, now)
	return ret, err
}

func (m *OrderMapper) TxOrderCancel(ctx context.Context, tx *sql.Tx, e *entity.OrderEntity) (sql.Result, error) {
	now := time.Now()
	query := fmt.Sprintf("update %s set `status`= ?,`update_time`= ? where `id` = ?", e.TableName())
	return tx.ExecContext(ctx, query, e.Status, now, e.Id)
}
